<template>
  <div class="bottom-bar">
    <div class="check-all">
      <check-botton :isChecked="isSelectAll" @click.native="checkAll" />
      <span>全选</span>
    </div>

    <div class="totalPrice">合计：¥&nbsp;{{ totalPrice }}</div>

    <div class="checkLength">去计算({{ checkLength }})</div>
  </div>
</template>

<script>
import CheckBotton from "../../../components/content/checkBotton/CheckBotton.vue";

export default {
  name: "CartBottomBar",
  components: {
    CheckBotton,
  },
  computed: {
    totalPrice() {
      return this.$store.state.cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((total, currentValue) => {
          return total + currentValue.price * currentValue.count;
        }, 0)
        .toFixed(2);
    },
    checkLength() {
      return this.$store.state.cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((total, currentValue) => {
          return total + currentValue.count;
        }, 0);
    },
    isSelectAll() {
      if (this.$store.state.cartList.length === 0) {
        return false;
      }
      return (
        this.$store.state.cartList.filter((item) => item.checked == false)
          .length === 0
      );
    },
  },
  methods: {
    checkAll() {
      if (this.isSelectAll == true) {
        this.$store.state.cartList.forEach((element) => {
          element.checked = false;
        });
      } else {
        this.$store.state.cartList.forEach((element) => {
          element.checked = true;
        });
      }
    },
  },
};
</script>

<style>
.bottom-bar {
  height: 40px;
  /* background-color: red; */
  position: fixed;
  bottom: 50px;
  right: 0;
  left: 0;
  line-height: 40px;
  display: flex;
  justify-content: space-between;
}
.check-all {
  display: flex;
  padding-left: 10px;
}
.totalPrice {
  padding-left: 10px;
}
.checkLength {
  width: 100px;
  line-height: 40px;
  text-align: center;
  color: aliceblue;
  font-size: 14px;
  background-color: orange;
}
</style>